package com.jp.knowledge.logic;

import android.content.Context;

//import com.anarchy.classify.model.TiyanApps;
//import com.anarchy.classify.model.TiyanData;

import com.jp.knowledge.model.experience.TiyanApps;
import com.jp.knowledge.model.experience.TiyanData;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.CreateGroup;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.util.L;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.List;

/**
 * Created by jiayen on 16/12/8.
 * <p>
 * 体验排序接口
 */

public class TiyanLogic implements ResultSubscriber.OnResultListener {
    private final static int SORT_EXP_CODE = 0x00001;//排序
    private final static int CREATE_GROUP_CODE = 0x00002;//创建组
    private final static int ADD_GROUP_CODE = 0x00003;//加入组
    private final static int DEL_MY_LAST_EXPERIENCE_CODE=0x00000000004;//获取最近体验列表
    private final static int RENAME_GROUP_CODE=0x00000000005;//重命名组
    private Context mContext;
    private CallBack callBack;
    private DelGroupCallBack delGroupCallBack;
    public TiyanLogic(Context context) {
        this.mContext = context;
    }


    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }


    public void setDelGroupCallBack(DelGroupCallBack delGroupCallBack) {
        this.delGroupCallBack = delGroupCallBack;
    }

    /**
     * 创建组
     */
    public void createGroup(String name){
        JsonObject param = new JsonObject();
        param.addProperty("name",name);
        HttpHelper.getInstance(mContext).expAddGroup(param,CREATE_GROUP_CODE,this);
    }

    /**
     * 加入组
     * @param id  组id
     * @param gid appid
     * @param op  操作   true是加入，false 是移除
     */
    public void addMyGroup(String id,JsonArray gid,boolean op){
        JsonObject param = new JsonObject();
        param.add("ids",gid);
        param.addProperty("gid",id);
        param.addProperty("op",op);
        HttpHelper.getInstance(mContext).expMyGroupAdd(param,ADD_GROUP_CODE,this);
    }
    /**
     * 排序组
     * @param tiyan
     */
    @Deprecated
    public void sortExpold(List<TiyanData> tiyan) {
        JsonObject param = new JsonObject();

        //拼接参数
        JsonArray dataArray = new JsonArray();
        for (TiyanData data: tiyan) {
            JsonObject  temp = new JsonObject();//临时存储
            List<TiyanApps> apps = data.getApps();
            if(apps!=null){
                int size =apps.size();
                if(size>1){//说明是组
                    JsonArray array = new JsonArray();
                    for (int i = 0; i < size; i++) {
                        JsonObject obj = new JsonObject();
                        obj.addProperty("mid",apps.get(i).getMid());
                        array.add(obj);//添加进组
                    }
                    temp.addProperty("mid",data.getMid());
                    temp.add("apps",array);
                }else {//说明是单个
                    temp.addProperty("mid",data.getMid());
                }
                dataArray.add(temp);
            }
        }
        param.add("apps",dataArray);
        HttpHelper.getInstance(mContext).expMySort(param, SORT_EXP_CODE, this);
    }


    /**
     * 排序组
     * @param list
     */
    public void sortExp(List<List<TiyanApps>> list) {
        JsonObject param = new JsonObject();

        //拼接参数
        JsonArray dataArray = new JsonArray();
        for (List<TiyanApps> apps: list) {
            JsonObject  temp = new JsonObject();//临时存储
            if(apps!=null){
                int size =apps.size();
                if(size>1){//说明是组
                    JsonArray array = new JsonArray();
                    String mid = "";
                    for (int i = 0; i < size; i++) {
                        JsonObject obj = new JsonObject();
                        mid = apps.get(i).getMid();
                        obj.addProperty("mid",mid);
                        array.add(obj);//添加进组
                    }
                    temp.addProperty("mid",mid);
                    temp.add("apps",array);
                }else {//说明是单个
                    temp.addProperty("mid",apps.get(0).getMid());
                }
                dataArray.add(temp);
            }
        }
        param.add("apps",dataArray);
        HttpHelper.getInstance(mContext).expMySort(param, SORT_EXP_CODE, this);
    }

    @Override
    public void onStart(int requestType) {
//        if(callBack!=null) callBack.error();
    }

    @Override
    public void onCompleted(int requestType) {
//        if(callBack!=null) callBack.error();
    }


    /**
     * 删除体验
     */
    public void delTIyanGroup(final String id){
        JsonObject obj = new JsonObject();
        obj.addProperty("id",id);
        HttpHelper.getInstance(mContext).expMyDelete(obj, DEL_MY_LAST_EXPERIENCE_CODE, this);
    }



    /**
     * 重命名组
     */
    public void groupRename(String gid, String groupName){
        JsonObject param = new JsonObject();
        param.addProperty("gid",gid);
        try {
            param.addProperty("name",groupName);
            HttpHelper.getInstance(mContext).expMyGroupRename(param,RENAME_GROUP_CODE,this);
        }catch (Exception e){
        }
    }
    @Override
    public void onError(int requestType) {
        if(callBack!=null) callBack.error();

        if(requestType==DEL_MY_LAST_EXPERIENCE_CODE&&requestType==DEL_MY_LAST_EXPERIENCE_CODE){
            IModel model = new IModel();
            model.setErrcode(-1);
            delGroupCallBack.delCompelete(model);
        }
    }

    @Override
    public void onNext(IModel data, int requestType) {
        if (requestType == SORT_EXP_CODE) {//排序完成
            L.e(""+data.getData());
        }else if(requestType==CREATE_GROUP_CODE){//创建完成
            if(callBack!=null){
            callBack.createCompelete(data.getEntity(CreateGroup.class));
            }
        }else if(requestType==ADD_GROUP_CODE){//加入完成
            if(callBack!=null){
                callBack.addCompelete();
            }
        }else if(requestType==DEL_MY_LAST_EXPERIENCE_CODE){
                if(delGroupCallBack!=null){
                    delGroupCallBack.delCompelete(data);
                }
        }
    }

    public interface CallBack{
        /**
         * 创建完成回调

         */
        void createCompelete(CreateGroup group);//创建完成

        /**
         * 加入成功回调
         */
        void addCompelete();
        void  error();
    }

    public interface  DelGroupCallBack{
        void delCompelete(IModel data);//删除完成
    }
}
